<script>
	$(function() {
		$( "#txtNgaythuchien" ).datepicker({dateFormat: 'dd-mm-yy'});
		$("#cboCountry").change(function() { 
		/* do something here */ 
			var ajax_url = '../incs/list_school_by_country_id.php?qg_id='+ $("#cboCountry").val();
			$.get(ajax_url,function(data){
			$('#cboSchool').html(data);
			});
		});
		$("#cboSchool").change(function() { 
		/* do something here */ 
			var ajax_url = '../incs/list_dept_by_school_id.php?truong_id='+ $("#cboSchool").val();
			$.get(ajax_url,function(data){
			$('#cboDept').html(data);
			});
		});
	});

		

</script>
<?php
if (!defined('WEB_ROOT')) {
	exit;
}

// make sure id exists
if (isset($_GET['id']) && $_GET['id'] > 0) {
	$id = (int)$_GET['id'];
} else {
	// redirect to index.php 
	header('Location: index.php');
}
/*
	$sql2 	= "select dv_id from hoithao where id = $id";
	
	$result2 = dbQuery($sql2) or die('' . mysql_error());
	
	if (mysql_num_rows($result2)!=0){
		while ($row = mysql_fetch_array($result2)){
			$dv_id = $row["dv_id"];
		}
	}*/
	
	$sql1 = "SELECT truong_id from hoithao ht, donvi dv where ht.dv_id = dv.id and ht.id = $id"	
	
	$result1 = dbQuery($sql1) or die('' . mysql_error());
	
	if (mysql_num_rows($result1)!=0){
		while ($row = mysql_fetch_array($result2)){
			$truong_id = $row["truong_id"];
		}
	}
			if ($truong_id > 0){
			// Lấy thông tin seminar
				$sql = "SELECT se.id, se.maht, se.tenht, chude, date_format(se.ngaythuchien,'%d-%m-%Y') as ngaythuchien1, se.diadiem, dv.tendv, dv_id, loaiht_id, 
					dv2.qg_id, dv2.id 					as dv2_id
					FROM hoithao se join donvi dv 
					on se.dv_id = dv.id join donvi dv2 on dv.truong_id = dv2.id join quocgia qg on dv2.qg_id = qg.id AND se.id = $id";
				}
				else {
				$sql = "SELECT se.id, se.maht, se.tenht, chude, date_format(se.ngaythuchien,'%d-%m-%Y') as ngaythuchien1, se.diadiem, dv.tendv as tentruong, 
					dv_id, loaiht_id, dv.qg_id
					FROM hoithao se join donvi dv 
					on se.dv_id = dv.id join quocgia qg on dv.qg_id = qg.id AND se.id = $id";
				}
	$result = mysql_query($sql) or die('Không lấy được thông tin seminar. ' . mysql_error());
	$row    = mysql_fetch_assoc($result);
	extract($row);	

		//combo loại hội thảo
	$sql = "SELECT id, tenloai
				FROM loaihoithao
				ORDER BY id";
		$result = dbQuery($sql) or die('Không lấy được hội thảo. ' . mysql_error());
		
		$groups = array();
		$list1 = '';
		while($row = dbFetchArray($result)) {
			list($id_loaiht, $name) = $row;
			$list1 .= "<option value=\"$id_loaiht\"";
				if ($id_loaiht == $loaiht_id) {
					$list1.= " selected";
				}
				$list1 .= ">$name</option>\r\n";
		}	
		//combo quốc gia
	$sql = "SELECT id, tenqg
				FROM quocgia
				ORDER BY id";
		$result = dbQuery($sql) or die('Không lấy được quốc gia. ' . mysql_error());
		
		$groups = array();
		$list4 = '';
		while($row = dbFetchArray($result)) {
			list($id_qg, $name) = $row;
			$list4 .= "<option value=\"$id_qg\"";
				if ($id_qg == $qg_id) {
					$list4.= " selected";
				}
				$list4 .= ">$name</option>\r\n";
		}	
		//combo trường
	$sql = "SELECT id, tendv
				FROM donvi where truong_id is NULL
				ORDER BY id";
		$result = dbQuery($sql) or die('Không lấy được trường. ' . mysql_error());
		
		$groups = array();
		$list3 = '';
		while($row = dbFetchArray($result)) {
			list($id_truong, $name) = $row;
			$list3 .= "<option value=\"$id_truong\"";
				if ($id_truong == $dv2_id) {
					$list3.= " selected";
				}
				$list3 .= ">$name</option>\r\n";
		}	

		//combo đơn vị
	$sql = "SELECT id, tendv
				FROM donvi
				ORDER BY id";
		$result = dbQuery($sql) or die('Không lấy được đơn vị. ' . mysql_error());
		
		$groups = array();
		$list2 = '';
		while($row = dbFetchArray($result)) {
			list($id_dv, $name) = $row;
			$list2 .= "<option value=\"$id_dv\"";
				if ($id_dv == $dv_id) {
					$list2.= " selected";
				}
				$list2 .= ">$name</option>\r\n";
		}	

?> 
<div class="onecolumn">
	<div class="header">
		<span>Sửa thông tin hội thảo</span>
	</div>
	<br class="clear"/>
	<div class="content">
		<form action="processSeminar.php?action=modifySeminar&id=<?php echo (int)$id; ?>" method="post" enctype="multipart/form-data" name="frmAddSeminar" id="frmAddSeminar">
		 <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
		  <tr> 
			   <td width="150" class="label">Mã hội thảo</td>
			   <td class="content"> <input name="txtMaht" type="text" class="box" id="txtMaht" readonly="true" style="background-color:#DADADA" value="<?php echo $maht; ?>"  size="20" maxlength="20"></td>
		  </tr>
		  <tr> 
			   <td width="150" class="label">Tên hội thảo</td>
			   <td class="content"> <input name="txtTenht" type="text" class="box" id="txtTenht" value="<?php echo $tenht; ?>"  size="80" maxlength="80"></td>
		  </tr>
		  <tr> 
			   <td width="150" class="label">Chủ đề</td>
			   <td class="content"><input name="txtChude" type="text" class="box" id="txtChude" value="<?php echo $chude; ?>"  size="80" maxlength="80"></td>
		  </tr>
		  <tr> 
			   <td width="150" class="label">Ngày thực hiện</td>
			   <td class="content"><input name="txtNgaythuchien" type="text" id="txtNgaythuchien" value="<?php echo $ngaythuchien1; ?>" size="40" maxlength="40" class="box"> </td>
		  </tr>
		  <tr> 
			   <td width="150" class="label">Địa điểm</td>
			   <td class="content"><input name="txtDiadiem" type="text" id="txtDiadiem" value="<?php echo $diadiem; ?>" size="40" maxlength="40" class="box"> </td>
		  </tr>
		  <tr> 
			   <td width="150" class="label">Quốc gia</td>
			   <td class="content"> <select name="cboCountry" id="cboCountry" class="box">
				 <option value="" selected>-- Chọn quốc gia --</option>
			<?php
				echo $list4;
			?>	 
				</select></td>
		  </tr>
		   <tr> 
			   <td width="150" class="label">Trường</td>
			   <td class="content"> <select name="cboSchool" id="cboSchool" class="box">
				 <option value="" selected>-- Chọn trường --</option>
			<?php
				echo $list3;
			?>	 
				</select></td>
		  </tr>
		  <tr> 
			   <td width="150" class="label">Đơn vị</td>
			   <td class="content"> <select name="cboDept" id="cboDept" class="box">
				 <option value="" selected>-- Chọn đơn vị --</option>
			<?php
				echo $list2;
			?>	 
				</select></td>
		  </tr>
		  <tr> 
		   <td width="150" class="label">Loại hội thảo</td>
		   <td class="content"> <select name="cboStyleSeminar" id="cboStyleSeminar" class="box">
			 <option value="" selected>-- Chọn loại hội thảo --</option>
		<?php
			echo $list1;
		?>	 
			</select></td>
		  </tr>
		 </table>
		 <p align="center"> 
		  <input name="btnModifySeminar" type="button" id="btnModifyProduct" value="Lưu thay đổi" onClick="checkAddSeminarForm();" class="box">
		  &nbsp;&nbsp;<input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.location.href='index.php';" class="box">  
		 </p>
		</form>
	</div>
</div>
